class RateCLI
  module Run
    def run
      algorithms = @client.list(target: 'algorithm')
      benchmarks = @client.list(target: 'benchmark')

      if algorithms.contents.empty? || benchmarks.contents.empty?
        info "No algorithms yet, or no benchmarks yet, create them!"
        return
      end

      begin
        loop do
          options = {}
          i = choose_from_harray("Choose an algorithm", algorithms.contents, %w{name uuid created desc})
          options[:algorithm] = algorithms.contents[i-1]
          i = choose_from_harray("Choose a benchmark", benchmarks.contents, %w{name uuid created desc})
          options[:benchmark] = benchmarks.contents[i-1]

          print_options(options)
          yes = $sui.ask_yes_no("Sure?")

          if yes
            options[:auuid] = options[:algorithm]['uuid']
            options[:buuid] = options[:benchmark]['uuid']
            result = @client.run(options)

            if result.success?
              success "Task #{short_uuid(result['uuid'])} created"
              break
            else
              error "can't run task, #{result.message}"
              yes = $sui.ask_yes_no "Try again?"
              break if not yes
            end
          end
        end
      rescue Interrupt => e
        puts
      end

    end
  end
end